<!DOCTYPE HTML>
<html lang="en">
<head>
<title>ControlSend[Raw] - Syntax &amp; Usage | AutoHotkey</title>
<meta name="description" content="The ControlSend and ControlSendRaw commands send simulated keystrokes to a window or control." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>ControlSend[Raw]</h1>

<p>Sends simulated keystrokes to a window or control.</p>

<pre class="Syntax">
<span class="func">ControlSend</span> <span class="optional">, Control, Keys, WinTitle, WinText, ExcludeTitle, ExcludeText</span>
ControlSendRaw: Same parameters as above.
</pre>
<h2>Parameters</h2>
<dl>

  <dt>Control</dt>
  <dd><p>Can be either ClassNN (the classname and instance number of the control) or the control's text, both of which can be determined via Window Spy. When using text, the matching behavior is determined by <a href="SetTitleMatchMode.htm">SetTitleMatchMode</a>. If this parameter is blank or omitted, the target window's topmost control will be used. If this parameter is <code>ahk_parent</code>, the keystrokes will be sent directly to the target window instead of one of its controls (see <a href="../misc/Winamp.htm">Automating Winamp</a> for an example).</p>
    <p>To operate upon a control's HWND (window handle), leave the <em>Control</em> parameter blank and specify <code>ahk_id %ControlHwnd%</code> for the <em>WinTitle</em> parameter (this also works on hidden controls even when <a href="DetectHiddenWindows.htm">DetectHiddenWindows</a> is Off). The HWND of a control is typically retrieved via <a href="ControlGet.htm#Hwnd">ControlGet Hwnd</a>, <a href="MouseGetPos.htm">MouseGetPos</a>, or <a href="DllCall.htm">DllCall()</a>.</p></dd>

  <dt>Keys</dt>
  <dd><p>The sequence of keys to send (see the <a href="Send.htm">Send</a> command for details). To send a literal comma, <a href="_EscapeChar.htm">escape</a> it (`,). The rate at which characters are sent is determined by <a href="SetKeyDelay.htm">SetKeyDelay</a>.</p>
    <p>Unlike the <a href="Send.htm">Send</a> command, mouse clicks cannot be sent by ControlSend. Use <a href="ControlClick.htm">ControlClick</a> for that.</p></dd>

  <dt>WinTitle</dt>
  <dd><p>A window title or other criteria identifying the target window. See <a href="../misc/WinTitle.htm">WinTitle</a>.</p></dd>

  <dt>WinText</dt>
  <dd><p>If present, this parameter must be a substring from a single text element of the target window (as revealed by the included Window Spy utility). Hidden text elements are detected if <a href="DetectHiddenText.htm">DetectHiddenText</a> is ON.</p></dd>

  <dt>ExcludeTitle</dt>
  <dd><p>Windows whose titles include this value will not be considered.</p></dd>

  <dt>ExcludeText</dt>
  <dd><p>Windows whose text include this value will not be considered.</p></dd>

</dl>

<h2>Error Handling</h2>
<p><span class="ver">[v1.1.04+]</span>: This command is able to throw an exception on failure. For more information, see <a href="Catch.htm#RuntimeErrors">Runtime Errors</a>.</p>
<p><a href="../misc/ErrorLevel.htm">ErrorLevel</a> is set to 1 if there was a problem or 0 otherwise.</p>
<h2>Remarks</h2>
<p>ControlSendRaw sends the keystrokes in the <em>Keys</em> parameter without translating <code>{Enter}</code> to <kbd>Enter</kbd>, <code>^c</code> to <kbd>Control</kbd>+<kbd>C</kbd>, etc. For details, see <a href="Send.htm#SendRaw">Raw mode</a>. It is also valid to use <a href="Send.htm#Raw">{Raw}</a> or <a href="Send.htm#Text">{Text}</a> with ControlSend. <span class="ver">[v1.1.27+]:</span> <a href="Send.htm#SendText">Text mode</a> may be more reliable for sending text.</p>
<p>If the <em>Control</em> parameter is omitted, this command will attempt to send directly to the target window by sending to its topmost control (which is often the correct one) or the window itself if there are no controls. This is useful if a window does not appear to have any controls at all, or just for the convenience of not having to worry about which control to send to.</p>
<p>By default, modifier keystrokes (<kbd>Control</kbd>, <kbd>Alt</kbd>, <kbd>Shift</kbd>, and <kbd>Win</kbd>) are sent as they normally would be by the Send command. This allows command prompt and other console windows to properly detect uppercase letters, control characters, etc. It may also improve reliability in other ways.</p>
<p>However, in some cases these modifier events may interfere with the active window, especially if the user is actively typing during a ControlSend or if the <kbd>Alt</kbd> key is being sent (since <kbd>Alt</kbd> activates the active window's menu bar). This can be avoided by explicitly sending modifier up and down events as in this example:</p>
<pre>ControlSend, Edit1, {Alt down}f{Alt up}, Untitled - Notepad</pre>
<p>The method above also allows the sending of modifier keystrokes (<kbd>Control</kbd>/<kbd>Alt</kbd>/<kbd>Shift</kbd>/<kbd>Win</kbd>) while the workstation is locked (protected by logon prompt).</p>
<p><a href="BlockInput.htm">BlockInput</a> should be avoided when using ControlSend against a console window such as command prompt. This is because it might prevent capitalization and modifier keys such as <kbd>Control</kbd> from working properly.</p>
<p>The value of <a href="SetKeyDelay.htm">SetKeyDelay</a> determines the speed at which keys are sent. If the target window does not receive the keystrokes reliably, try increasing the press duration via the second parameter of <a href="SetKeyDelay.htm">SetKeyDelay</a> as in these examples:</p>
<pre>SetKeyDelay, 10, 10
SetKeyDelay, 0, 10
SetKeyDelay, -1, 0</pre>
<p>If the target control is an Edit control (or something similar), the following are usually more reliable and faster than ControlSend:</p>
<pre><a href="Control.htm">Control</a>, EditPaste, This text will be inserted at the caret position., ControlName, WinTitle</pre>
<pre><a href="ControlSetText.htm">ControlSetText</a>, ControlName, This text will entirely replace any current text., WinTitle</pre>
<p>ControlSend is generally not capable of manipulating a window's menu bar. To work around this, use <a href="WinMenuSelectItem.htm">WinMenuSelectItem</a>. If that is not possible due to the nature of the menu bar, you could try to discover the message that corresponds to the desired menu item by following the <a href="../misc/SendMessage.htm">SendMessage Tutorial</a>.</p>
<p>Window titles and text are case sensitive. Hidden windows are not detected unless <a href="DetectHiddenWindows.htm">DetectHiddenWindows</a> has been turned on.</p>
<h2>Related</h2>
<p><a href="SetKeyDelay.htm">SetKeyDelay</a>, <a href="_EscapeChar.htm">Escape sequences (e.g. `%)</a>, <a href="Control.htm">Control</a>, <a href="ControlGet.htm">ControlGet</a>, <a href="ControlGetText.htm">ControlGetText</a>, <a href="ControlMove.htm">ControlMove</a>, <a href="ControlGetPos.htm">ControlGetPos</a>, <a href="ControlClick.htm">ControlClick</a>, <a href="ControlSetText.htm">ControlSetText</a>, <a href="ControlFocus.htm">ControlFocus</a>, <a href="Send.htm">Send</a>, <a href="../misc/Winamp.htm">Automating Winamp</a></p>
<h2>Examples</h2>
<div class="ex" id="ExNotepad">
<p><a href="#ExNotepad">#1</a>: Open Notepad (minimized) and send it some text.</p>
<pre>Run, Notepad,, Min, PID  <em>; Run Notepad minimized.</em>
WinWait, ahk_pid %PID%  <em>; Wait for it to appear.</em>
<em>; Send the text to the inactive Notepad edit control.
; The third parameter is omitted so the last found window is used.</em>
ControlSend, Edit1, This is a line of text in the notepad window.{Enter}
ControlSendRaw, Edit1, Notice that {Enter} is not sent as an Enter keystroke with ControlSendRaw.

MsgBox, Press OK to activate the window to see the result.
WinActivate, ahk_pid %PID%  <em>; Show the result.</em></pre>
</div>

<div class="ex" id="ExCmd">
<p><a href="#ExCmd">#2</a>: Open the command prompt and sent it some text.</p>
<pre>SetTitleMatchMode, 2
Run, %A_ComSpec%,,, PID  <em>; Run command prompt.</em>
WinWait, ahk_pid %PID%  <em>; Wait for it to appear.</em>
ControlSend,, ipconfig{Enter}, cmd.exe  <em>; Send directly to the command prompt window.</em></pre>
</div>

</body>
</html>
